<?php

namespace App\Console\Commands;

use App\Models\Circle;
use App\Models\Star;
use App\Models\User;
use App\Services\UserWeightService;
use Illuminate\Console\Command;
use YangJiSen\CacheUserProvider\CacheForget;

class UserWeightCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user-weight';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        \Log::info('-----------user-weight-----------------------------');
        User::chunk(50, function ($list) {
            $start_at = date("Y-m-d 00:00:01",strtotime("-2 day"));
             foreach ($list as $user) {
                 UserWeightService::handle($user, $start_at);
                 //获取当前用户的被点赞数
                 $user->stared_count = Star::where('entity_user_id', $user->id)->count();
                 $user->star_count = Star::where('user_id', $user->id)->count();
                 $user->save();
                 CacheForget::CacheForget($user);
             }
        });
        User::where('top_hot_search_height_100', 1)->chunkById(100, function ($list) {
            foreach ($list as $user) {
                $user->top_hot_search_height_100 = 0;
                $user->save();
                CacheForget::CacheForget($user);
            }
        });

        $arr_id = User::orderBy('hot_search_weight', 'desc')->limit(100)->pluck('id')->all();
        User::whereIn('id', $arr_id)->chunkById(100, function ($list) {
            foreach ($list as $user) {
                $user->top_hot_search_height_100 = 1;
                $user->save();
                CacheForget::CacheForget($user);
            }
        });
        \Cache::forget('hot_search_user_key');
        \Log::info('-----------end-user-weight-----------------------------');
    }
}
